package de.cellamaro.guestbook.logic;

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import de.cellamaro.guestbook.model.Entry;

/**
 * @author cellamaro
 *
 */
@ManagedBean(name="GuestbookBean")
@SessionScoped
public class Guestbook {

	public Entry entry;
	
	public Guestbook(){
		entry = new Entry();
	}
	
	/**
	 * @return the entry
	 */
	public Entry getEntry() {
		return entry;
	}

	/**
	 * @param entry the entry to set
	 */
	public void setEntry(Entry entry) {
		this.entry = entry;
	}

	public String getEntries(){
		Configuration configuration=new Configuration();  
		configuration.configure();  
		ServiceRegistry sr= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();  
		SessionFactory sf=configuration.buildSessionFactory(sr);
		
		Session session = sf.openSession();
		List<Entry> result = (List<Entry>) session.createCriteria(Entry.class).list();
		
		String o = "";
		for (int i = 0; i < result.size(); i++){
			Entry c = result.get(i);
			o += c.getDate()+" "+c.getName()+" <br />";
			o += c.getMessage()+"<br />";
			o += "<hr />";
		}
		o += "<br />";
		return o;
	}
	
	public List<Entry> getEntryList(){
		Configuration configuration=new Configuration();  
		configuration.configure();  
		ServiceRegistry sr= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();  
		SessionFactory sf=configuration.buildSessionFactory(sr);
		
		Session session = sf.openSession();
		List<Entry> result = (List<Entry>) session.createCriteria(Entry.class).list();
		
		return result;
	}
	
	public void addComment(){
		Configuration configuration=new Configuration();  
		configuration.configure();  
		ServiceRegistry sr= new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();  
		SessionFactory sf=configuration.buildSessionFactory(sr);
		
		Session session = sf.openSession();
		entry.setDate(new Date());
		
		session.beginTransaction();
		session.save(entry);
		session.getTransaction().commit();
		
		session.close();
		
		entry = new Entry();
	}
	
	// OffTopic, but interesting
	public String getSystemStuff(){
		String output = "";
		OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();

		  for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
		    method.setAccessible(true);
		    if (method.getName().startsWith("get") 
		        && Modifier.isPublic(method.getModifiers())) {
		            Object value;
		        try {
		            value = method.invoke(operatingSystemMXBean);
		        } catch (Exception e) {
		            value = e;
		        } // try
		        output += method.getName() + " = " + value+"\n<br />";
		    } // if
		  } // for
		  
		  return output;
	}
	
}
